//
//  AddressModel.m
//  Wallet
//
//  Created by mark-work on 2018/4/6.
//  Copyright © 2018年 WWJ. All rights reserved.
//

#import "AddressModel.h"
#import "UIWindow+PazLabs.h"
@interface AddressModel()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, copy) void (^selectCellBlock)(AddressModel *model, NSInteger index);
@property (nonatomic, copy) void (^cancelActionBlock)(void);
@property (nonatomic, copy) void (^addActionBlock)(void);

@property (nonatomic, strong) NSArray *itemArray;

@end
static UIView *backGroundView;
static UIView *_maskingView;
static AddressModel *meumView;

@implementation AddressModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
    if ([key isEqualToString:@"id"]) {
        self.coin_id = value;
    }
}
//@property (nonatomic,copy) NSString *address;
//@property (nonatomic,copy) NSString *coin_name;
//@property (nonatomic,copy) NSString *coin_id;
//@property (nonatomic,copy) NSString *type;

//归档(序列化)的协议,将属性转换为NSData的时候
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.address forKey:@"address"];
    [aCoder encodeObject:self.coin_name forKey:@"coin_name"];
    [aCoder encodeObject:self.coin_id forKey:@"coin_id"];
    [aCoder encodeObject:self.type forKey:@"type"];

}
//反归档(反序列化),将NSData转换为Model的时候,将对应的属性进行赋值
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.address = [aDecoder decodeObjectForKey:@"address"];
        self.coin_name = [aDecoder decodeObjectForKey:@"coin_name"];
        self.coin_id = [aDecoder decodeObjectForKey:@"coin_id"];
        self.type = [aDecoder decodeObjectForKey:@"type"];

    }
    return self;
}

+ (void)saveAddressModel:(AddressModel *)model{
    if (!model) {
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"addressModel"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        return;
    }
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
    [userDefaults setObject:data forKey:@"addressModel"];
    [userDefaults synchronize];
}
+ (AddressModel *)getAddressModel{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [userDefaults objectForKey:@"addressModel"];

    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
+(void)removeModel{
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"addressModel"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}
+(instancetype)insatanceMeumView{
    if (!meumView) {
        meumView = [[AddressModel alloc]init];
        
    }
    return meumView;
}

+(UIView *)shareInsatanceMaskingView{
    if (!_maskingView) {
        _maskingView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _maskingView;
}

+ (void)showPromptBoxsViewWithFrame:(CGRect )frame vc:(UIViewController *)vc withItems:(NSArray <AddressModel *>*)items withSelectItem:(void(^)(AddressModel *model, NSInteger index))selectItem cancelAction:(void(^)(void))cancel withAddClick:(void(^)(void))addClick{
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    if (window.visibleViewController != vc) {
        return;
    }
    meumView = [AddressModel insatanceMeumView];
    meumView.itemArray = items;
    if (!backGroundView) {
        backGroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        
        _maskingView = [AddressModel shareInsatanceMaskingView];
        [backGroundView addSubview:_maskingView];
        
        UITapGestureRecognizer *hideTap=[[UITapGestureRecognizer alloc]initWithTarget:meumView action:@selector(cancelAction)];
        hideTap.numberOfTapsRequired = 1;
        [_maskingView addGestureRecognizer: hideTap];
        
        UITableView *tableView = [[UITableView alloc]initWithFrame:frame];
        tableView.separatorStyle = 2;
        tableView.separatorColor = kLineColor;
        tableView.backgroundColor = kBtnColor;
        tableView.delegate = meumView;
        tableView.dataSource = meumView;
        tableView.layer.borderWidth = 0.5;
        tableView.layer.borderColor = kSetColor(@"2e3947").CGColor;
//        tableView.layer.cornerRadius = 4;
//        tableView.layer.masksToBounds = YES;
        [backGroundView addSubview:tableView];
        
        
        
        [window addSubview:backGroundView];
        
        meumView.selectCellBlock = ^(AddressModel *model, NSInteger index) {
            if (selectItem) {
                selectItem(model,index);
            }
        };
        meumView.cancelActionBlock = ^{
            if (cancel) {
                cancel();
            }
        };
        meumView.addActionBlock = ^{
            if (addClick) {
                addClick();
            }
        };
    }
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return meumView.itemArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressModel *model = meumView.itemArray[indexPath.row];
    static NSString *ID = @"MeumCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.textLabel.font = FONT(13);
        cell.detailTextLabel.font = FONT(12);
       cell.backgroundColor= cell.contentView.backgroundColor = kBackColor;
        cell.textLabel.textColor = kTradeWhiteColor;
        cell.detailTextLabel.textColor = kTradeWhiteColor;

    }
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.address;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    meumView = [AddressModel insatanceMeumView];
    AddressModel *model = meumView.itemArray[indexPath.row];
    if (meumView.selectCellBlock) {
        meumView.selectCellBlock(model, indexPath.row);
    }
    
    [AddressModel clear];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  44;
}

- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, tableView.width, 40)];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:kImageName(@"add") forState:UIControlStateNormal];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
    btn.titleLabel.font = FONT(15);
    
//    [btn setTitle:kGetStr(@"Add a new address") forState:UIControlStateNormal];
    [btn setTitleColor:kTradeWhiteColor forState:UIControlStateNormal];
//    UIImageView *line = [UIImageView createLineWithColor:kTradeLineColor frame:CGRectMake(0, 0, btn.width, 0.5)];
    btn.backgroundColor = kBackColor;
//    [btn addSubview:line];
    return btn;
}
- (void)btnAction{
    meumView = [AddressModel insatanceMeumView];
    if (meumView.addActionBlock) {
        meumView.addActionBlock();
    }
    [AddressModel clear];
}
- (void)cancelAction{
    meumView = [AddressModel insatanceMeumView];
    if (meumView.cancelActionBlock) {
        meumView.cancelActionBlock();
    }
    
    [AddressModel clear];
    
}

+ (void)clear{
    [backGroundView removeFromSuperview];
    backGroundView = nil;
    _maskingView=nil;
    meumView = nil;
}
@end
